/**
 * 
 */
package com.mc.okhttp;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;

import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;  
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;  
import java.net.URLConnection;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @author w.weizhen
 *
 */
public class HttpRequestUtils 
{
	public static void main(String[] args) {
//        // String url = "http://192.168.200.117:8087/v2/servers/_defaultServer_/vhosts/_defaultVHost_/applications/live/publishers";
//        // String url = "http://192.168.200.117:8087/v2/servers/_defaultServer_/vhosts/_defaultVHost_/applications/Relay/streamfiles/1234566/actions/connect?&vhost=_defaultVHost_&appType=live&appName=Relay&appInstance=_definst_&connectAppName=Relay&connectAppInstance=_definst_&streamFile=1234566.stream&mediaCasterType=liverepeater";
//        String param = "";
//        String username = "xxxxxx";
//        String password = "xxxxxx";
//        // String json = "{ \"password\": \"plmo13579123\", \"publisherName\": \"4\", \"serverName\": \"_defaultServer_\", \"description\": \"\", \"saveFieldList\": [ \"\" ], \"version\": \"v1.0\" }";
//        // String s = sendPost(url, param, username, password, json);
//
//        // String s = sendPost(url, param, username, password, json);
//
//        // -----------------GET-success------------------
//        String getUrl = "http://192.168.200.117:8087/v2/servers/_defaultServer_/vhosts/_defaultVHost_/applications";
//        // String s = sendGet(getUrl, param, username, password, null);
//        // -----------------GET-success------------------
//
//        // -----------------PUT-success------------------
//        String putUrl = "http://192.168.200.117:8087/v2/servers/_defaultServer_/vhosts/_defaultVHost_/applications/Relay/streamfiles/6D07D7E7623B95889C33DC5901307461_0/actions/connect";
//        String putJson = "{ \"vhost\":\"_defaultVHost_\", \"mediaCasterType\":\"liverepeater\" }";
//        // String s = sendPUT(putUrl, param, username, password, putJson, null);
//        // -----------------PUT-success------------------
//
//        // -----------------POST-success------------------
//        String postUrl = "http://192.168.200.117:8087/v2/servers/_defaultServer_/users";
//        String postJson = "{ \"password\": \"123456\", \"serverName\": \"_defaultServer_\", \"description\": \"\", \"groups\": [ \"\" ], \"saveFieldList\": [ \"\" ], \"userName\": \"test6\", \"version\": \"v1.0\" }";
//        // String s = sendPost(postUrl, param, username, password, postJson, null);
//        // -----------------POST-success------------------
//
//        // -----------------POST-success------------------
//        String postUrl2 = "http://192.168.200.117:8087/v2/servers/_defaultServer_/publishers";
//        String postJson2 = "{ \"password\": \"1579655633@qq.com\", \"name\": \"test11\", \"serverName\": \"_defaultServer_\", \"description\": \"test\", \"saveFieldList\": [ \"\" ], \"version\": \"v1.0\" }";
//        // String s = sendPost(postUrl2, param, username, password, postJson2, null);
//        // -----------------POST-success------------------
//
//        // -----------------DELETE-success------------------
//        String deleteUrl = "http://192.168.200.117:8087/v2/servers/_defaultServer_/users/test5";
//        // String deleteJson = "{ \"password\": \"1579655633@qq.com\", \"name\": \"test11\", \"serverName\": \"_defaultServer_\", \"description\": \"test\", \"saveFieldList\": [ \"\" ], \"version\": \"v1.0\" }";
//        String s = sendDelete(deleteUrl, param, username, password, null, null);
//        // -----------------DELETE-success------------------
        
		
		// String url = "http://192.168.200.117:8087/v2/servers/_defaultServer_/vhosts/_defaultVHost_/applications/live/publishers";
		String url = "";
		String param = "";
        String json = "";
        String username = "admin";
        String password = "Password_002";
        String s = "";
        
//		//获取设备列表
//		String post_url = "http://192.168.12.11:80/ISAPI/ContentMgmt/DeviceMgmt/deviceList?format=json";
//		json = "{\"SearchDescription\":{\"position\":0,\"maxResult\":100,\"Filter\":{\"key\":\"\",\"devType\":\"\",\"protocolType\":[\"ehomeV5\"],\"devStatus\":[\"online\",\"offline\"]}}}";
//		s = sendPost(post_url, param, username, password, json, null);
//        System.out.println(s);
		
//        //安全认证
//      	String get_url = "http://192.168.12.11:80/ISAPI/Security/userCheck?format=json";
//		s = sendGet(get_url, param, username, password, null);
//        System.out.println(s);
        
//        //获取设备信息
//        get_url = "http://192.168.12.11:80/ISAPI/System/deviceInfo?format=json&devIndex=1A1E4093-8441-4766-8A69-156B8DD88AD1";
//        s = sendGet(get_url, param, username, password, null);
//        System.out.println(s);
        
//		//添加人员
//		post_url = "http://192.168.12.11:80/ISAPI/AccessControl/UserInfo/Record?format=json&devIndex=1A1E4093-8441-4766-8A69-156B8DD88AD1";
//		json = "{\"UserInfo\":[{\"employeeNo\":\"1672195548\",\"name\":\"宣玉邦\",\"Valid\":{\"beginTime\":\"2022-12-01T00:00:00\",\"endTime\":\"2032-12-31T23:59:59\"}}]}";
//		s = sendPost(post_url, param, username, password, json, null);
//        System.out.println(s);
        
//        //删除人员
//        url = "http://192.168.12.11:80/ISAPI/AccessControl/UserInfoDetail/Delete?format=json&devIndex=1A1E4093-8441-4766-8A69-156B8DD88AD1";
//        json = "{\"UserInfoDetail\":{\"mode\":\"byEmployeeNo\",\"EmployeeNoList\":[{\"employeeNo\":\"1672195548\"}]}}";
//        s = sendPUT(url, param, username, password, json, null);
//        System.out.println(s);
        
//        //修改人员
//        url = "http://192.168.12.11:80/ISAPI/AccessControl/UserInfo/Modify?format=json&devIndex=1A1E4093-8441-4766-8A69-156B8DD88AD1";
//        json = "{\"UserInfo\":{\"employeeNo\":\"1671524742\",\"name\":\"孔鸿风\",\"Valid\":{\"beginTime\":\"2020-01-01T00:00:00\",\"endTime\":\"2030-12-31T23:59:59\"}}}";
//        s = sendPUT(url, param, username, password, json, null);
//        System.out.println(s);
        
//        //添加人脸
//        url = "http://192.168.12.11:80/ISAPI/Intelligent/FDLib/FaceDataRecord?format=json&devIndex=1A1E4093-8441-4766-8A69-156B8DD88AD1";
//		json = "{\"FaceInfo\":{\"employeeNo\":\"1672195548\"}}";
//		s = faceDataRecord(url, param, username, password, json, "json");
//        System.out.println(s);
        
//        //删除人脸
//        url = "http://192.168.12.11:80/ISAPI/Intelligent/FDLib/FDSearch/Delete?format=json&devIndex=1A1E4093-8441-4766-8A69-156B8DD88AD1";
//        json = "{\"FaceInfoDelCond\":{\"EmployeeNoList\":[{\"employeeNo\":\"1672195548\"}]}}";
//        s = sendPUT(url, param, username, password, json, null);
//        System.out.println(s);
        
//		//添加卡[这个是多余的]
//		String post_url = "http://192.168.12.11:80/ISAPI/AccessControl/CardInfo/Record?format=json&devIndex=1A1E4093-8441-4766-8A69-156B8DD88AD1";
//		json = "{\"CardInfo\":{\"employeeNo\":\"1672122569\",\"cardNo\":\"1672123264\"}}";
//		s = sendPost(post_url, param, username, password, json, null);
//        System.out.println(s);
        
		//查询人员列表
//        {
//        	"UserInfoSearchCond": {
//        		"searchID": "",
//        		/*必选项，string，查询 ID，用于确认前后两次的搜索请求者是否相同；如果相同，搜索记录将被存储在设备中以加快下一次搜索*/
//        		"searchResultPosition": 0,
//        		/*必选项，integer32，搜索结果在结果列表的结束位置；如果在一次查询中，不能获取结果列表中的所有记录,可以标记结束位置,并在下次查询时直接获取到指定位置后的记录*/
//        		"maxResults": 30,
//        		/*必选项，integer32，可获取的最大记录数；如 maxResults 的值大于设备能力集返回的范围，则设备按照能力集最大值返回，不进行报错*/
//        		"EmployeeNoList": [{
//        			/*可选项，人员 ID 列表；当该节点不存在或者未配置是，表示将查询所有人员*/
//        			"employeeNo": ""
//        			/*可选项，string，工号（人员 ID）*/
//        		}]
//        	}
//        }
//		post_url = "http://192.168.12.11:80/ISAPI/AccessControl/UserInfo/Search?format=json&devIndex=1A1E4093-8441-4766-8A69-156B8DD88AD1";
//		json = "{\"UserInfoSearchCond\":{\"searchID\":\"C7E71364-4560-0001-6EDD-16ED17B01CCD\",\"searchResultPosition\":0,\"maxResults\":3000}}";
//		s = sendPost(post_url, param, username, password, json, null);
//        System.out.println(s);
        
//        //查询历史门禁事件
        // 更多详情查看 UD29330B_Hik Device Gateway API_开发者指南_1.6_20221202.PDF
//        {
//        	"AcsEventCond": {
//        		"searchID": "", /*必选项，string，查询 ID，用于确认前后两次的搜索请求者是否相同；如果相同，搜索记录将被存储在设备中以加快下一次搜索*/
//        		"searchResultPosition": , /*必选项，integer32，搜索结果在结果列表的结束位置；如果在一次查询中，不能获取结果列表中的所有记录,可以标记结束位置,并在下次查询时直接获取到指定位置后的记录*/
//        		"maxResults": , /*必选项，integer32，可获取的最大记录数，依赖于设备能力。若 maxResults 值大于设备能力集返回的范围，则设备按照能力集最大值返回，不进行报错*/
//        		"major": , /*可选项，int，事件主类型，参考事件上传宏定义，此处传递的是 10 进制数据，不能传递 16 进制，如传递1024 代表 0x400。不填默认为 0，表示全部事件/
//        		"minor": , /*可选项，int，事件次类型，参考事件上传宏定义，此处传递的是 10 进制数据，不能传递 16 进制，如传递1024 代表 0x400。不填默认为 0，表示全部事件/
//        		"startTime": "2016-12-12T17:30:08+08:00", /*可选项，string，开始时间（UTC 时间）*/
//        		"endTime": "2017-12-12T17:30:08+08:00", /*可选项，string，结束时间（UTC 时间）*/
//        		"cardNo": "", /*可选项，string，卡号*/
//        		"name": "", /*可选项，string，持卡人姓名*/
//        		"picEnable": , /*可选项，boolean，是否带图片：false（不带图片）、true（带图片）*/
//        		"employeeNo": "" /*可选项，string，工号（人员 ID）*/
//        	}
//        }
//        post_url = "http://192.168.12.11:80/ISAPI/AccessControl/AcsEvent?format=json&devIndex=1A1E4093-8441-4766-8A69-156B8DD88AD1";
//        String startTime = DateUtil.format(DateUtil.offset(DateUtil.date(), DateField.HOUR_OF_DAY, -1), "yyyy-MM-dd'T'HH:mm:ss")+"+08:00";
//		json = "{\"AcsEventCond\":{\"searchID\":\""+System.currentTimeMillis()+"\",\"searchResultPosition\":0,\"maxResults\":10,\"startTime\":\""+startTime+"\",\"picEnable\": true}}";
//		s = sendPost(post_url, param, username, password, json, null);
//        System.out.println(s);
        
//        //远程控制门
//        url = "http://192.168.12.11:80/ISAPI/AccessControl/RemoteControl/door/1?format=json&devIndex=1A1E4093-8441-4766-8A69-156B8DD88AD1";
//        json = "{\"RemoteControlDoor\":{\"cmd\":\"open\"}}";
//        s = sendPUT(url, param, username, password, json, null);
//        System.out.println(s);
        
        
        
    }

    static int nc = 0;    //调用次数
    private static final String GET = "GET";
    private static final String POST = "POST";
    private static final String PUT = "PUT";
    private static final String DELETE = "DELETE";

    /**
     * 向指定URL发送DELETE方法的请求
     * @param url                                   发送请求的URL
     * @param param                                 请求参数，请求参数应该是 name1=value1&name2=value2 的形式。
     * @param username                              验证所需的用户名
     * @param password                              验证所需的密码
     * @param json                                  请求json字符串
     * @param type                                  返回xml和json格式数据，默认xml，传入json返回json数据
     * @return URL                                  所代表远程资源的响应结果
     */
    public static String sendDelete(String url, String param, String username, String password, String json, String type) {

        StringBuilder result = new StringBuilder();
        BufferedReader in = null;
        try {
            String wwwAuth = sendGet(url, param);       //发起一次授权请求
            if (wwwAuth.startsWith("WWW-Authenticate:")) {
                wwwAuth = wwwAuth.replaceFirst("WWW-Authenticate:", "");
            } else {
                return wwwAuth;
            }
            nc ++;
            String urlNameString = url + (StringUtils.isNotEmpty(param) ? "?" + param : "");
            URL realUrl = new URL(urlNameString);
            // 打开和URL之间的连接
            HttpURLConnection connection = (HttpURLConnection)realUrl.openConnection();

            // 设置是否向connection输出，因为这个是post请求，参数要放在
            // http正文内，因此需要设为true
            connection.setDoOutput(true);
            // Read from the connection. Defaultis true.
            connection.setDoInput(true);
            // 默认是 GET方式
            connection.setRequestMethod(DELETE);

            // 设置通用的请求属性
            setRequestProperty(connection, wwwAuth, realUrl, username, password, DELETE, type);

            if (!StringUtils.isEmpty(json)) {
                byte[] writebytes =json.getBytes();
                connection.setRequestProperty("Content-Length",String.valueOf(writebytes.length));
                OutputStream outwritestream = connection.getOutputStream();
                outwritestream.write(json.getBytes());
                outwritestream.flush();
                outwritestream.close();
            }

            if (connection.getResponseCode() == 200) {
                in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String line;
                while ((line = in.readLine()) != null) {
                    result.append(line);
                }
            } else {
                String errResult = formatResultInfo(connection, type);
                return errResult;
            }

            nc = 0;
        } catch (Exception e) {
            nc = 0;
            throw new RuntimeException(e);
        } finally {
            try {
                if (in != null) in.close();
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return result.toString();
    }



    /**
     * 向指定URL发送PUT方法的请求
     * @param url                                      发送请求的URL
     * @param param                                    请求参数，请求参数应该是 name1=value1&name2=value2 的形式。
     * @param username                                 验证所需的用户名
     * @param password                                 验证所需的密码
     * @param json                                     请求json字符串
     * @param type                                     返回xml和json格式数据，默认xml，传入json返回json数据
     * @return URL                                     所代表远程资源的响应结果
     */
    public static String sendPUT(String url, String param, String username, String password, String json, String type) {

        StringBuilder result = new StringBuilder();
        BufferedReader in = null;
        try {
            String wwwAuth = sendGet(url, param);       //发起一次授权请求
            if (wwwAuth.startsWith("WWW-Authenticate:")) {
                wwwAuth = wwwAuth.replaceFirst("WWW-Authenticate:", "");
            } else {
                return wwwAuth;
            }
            nc ++;
            String urlNameString = url + (StringUtils.isNotEmpty(param) ? "?" + param : "");
            URL realUrl = new URL(urlNameString);
            // 打开和URL之间的连接
            HttpURLConnection connection = (HttpURLConnection)realUrl.openConnection();

            // 设置是否向connection输出，因为这个是post请求，参数要放在
            // http正文内，因此需要设为true
            connection.setDoOutput(true);
            // Read from the connection. Defaultis true.
            connection.setDoInput(true);
            // 默认是 GET方式
            connection.setRequestMethod(PUT);
            // Post 请求不能使用缓存
            connection.setUseCaches(false);

            // 设置通用的请求属性
            setRequestProperty(connection, wwwAuth,realUrl, username, password, PUT, type);

            if (!StringUtils.isEmpty(json)) {
                byte[] writebytes =json.getBytes();
                connection.setRequestProperty("Content-Length",String.valueOf(writebytes.length));
                OutputStream outwritestream = connection.getOutputStream();
                outwritestream.write(json.getBytes());
                outwritestream.flush();
                outwritestream.close();
            }

            if (connection.getResponseCode() == 200) {
                in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String line;
                while ((line = in.readLine()) != null) {
                    result.append(line);
                }
            } else {
                String errResult = formatResultInfo(connection, type);
                return errResult;
            }

            nc = 0;
        } catch (Exception e) {
            nc = 0;
            throw new RuntimeException(e);
        } finally {
            try {
                if (in != null) in.close();
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return result.toString();
    }

    /**
     * 向指定URL发送POST方法的请求
     * @param url                                       发送请求的URL
     * @param param                                     请求参数，请求参数应该是 name1=value1&name2=value2 的形式。
     * @param username                                  验证所需的用户名
     * @param password                                  验证所需的密码
     * @param json                                      请求json字符串
     * @param type                                      返回xml和json格式数据，默认xml，传入json返回json数据
     * @return URL 所代表远程资源的响应结果
     */
    public static String sendPost(String url, String param, String username, String password, String json, String type) {

        StringBuilder result = new StringBuilder();
        BufferedReader in = null;
        try {
            String wwwAuth = sendGet(url, param);       //发起一次授权请求
            if (wwwAuth.startsWith("WWW-Authenticate:")) {
                wwwAuth = wwwAuth.replaceFirst("WWW-Authenticate:", "");
            } else {
                return wwwAuth;
            }
            nc ++;
            String urlNameString = url + (StringUtils.isNotEmpty(param) ? "?" + param : "");
            URL realUrl = new URL(urlNameString);
            // 打开和URL之间的连接
            HttpURLConnection connection = (HttpURLConnection)realUrl.openConnection();

            // 设置是否向connection输出，因为这个是post请求，参数要放在
            // http正文内，因此需要设为true
            connection.setDoOutput(true);
            // Read from the connection. Defaultis true.
            connection.setDoInput(true);
            // 默认是 GET方式
            connection.setRequestMethod(POST);
            // Post 请求不能使用缓存
            connection.setUseCaches(false);

            // 设置通用的请求属性
            setRequestProperty(connection, wwwAuth,realUrl, username, password, POST, type);

            if (!StringUtils.isEmpty(json)) {
                byte[] writebytes =json.getBytes();
                connection.setRequestProperty("Content-Length",String.valueOf(writebytes.length));
                OutputStream outwritestream = connection.getOutputStream();
                outwritestream.write(json.getBytes());
                outwritestream.flush();
                outwritestream.close();
            }
            if (connection.getResponseCode() == 200 || connection.getResponseCode() == 201) {
                in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String line;
                while ((line = in.readLine()) != null) {
                    result.append(line);
                }
            } else {
                String errResult = formatResultInfo(connection, type);
                return errResult;
            }

            nc = 0;
        } catch (Exception e) {
            nc = 0;
            throw new RuntimeException(e);
        } finally {
            try {
                if (in != null) in.close();
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return result.toString();
    }
    
    
	 /**
	  * 向指定URL发送POST方法的请求
	  * @param url                                       发送请求的URL
	  * @param param                                     请求参数，请求参数应该是 name1=value1&name2=value2 的形式。
	  * @param username                                  验证所需的用户名
	  * @param password                                  验证所需的密码
	  * @param json                                      请求json字符串
	  * @param type                                      返回xml和json格式数据，默认xml，传入json返回json数据
	  * @return URL 所代表远程资源的响应结果
	  */
	 public static String faceDataRecord(String url, String param, String username, String password, String json, String type) {
	     StringBuilder result = new StringBuilder();
	     BufferedReader in = null;
	     try {
	         String wwwAuth = sendGet(url, param);       //发起一次授权请求
	         if (wwwAuth.startsWith("WWW-Authenticate:")) {
	             wwwAuth = wwwAuth.replaceFirst("WWW-Authenticate:", "");
	         } else {
	             return wwwAuth;
	         }
	         nc ++;
	         String urlNameString = url + (StringUtils.isNotEmpty(param) ? "?" + param : "");
	         URL realUrl = new URL(urlNameString);
	         // 打开和URL之间的连接
	         HttpURLConnection connection = (HttpURLConnection)realUrl.openConnection();
	         
	         // 设置是否向connection输出，因为这个是post请求，参数要放在
	         // http正文内，因此需要设为true
	         connection.setDoOutput(true);
	         // Read from the connection. Defaultis true.
	         connection.setDoInput(true);
	         // 默认是 GET方式
	         connection.setRequestMethod(POST);
	         // Post 请求不能使用缓存
	         connection.setUseCaches(false);
	         
	         // 设置通用的请求属性
	         setRequestProperty(connection, wwwAuth,realUrl, username, password, POST, type);
	         
	         
	         byte[] file = Files.readAllBytes(Paths.get("D:\\workSpace\\file\\612f421ad0eec21632f46077fe141596.jpg")); // 文件内容
	         
	         String BOUNDARY = "---------------"+RandomStringUtils.random(15, "12345678901234567890abcef12345678901234567890"); // 分隔符
	         StringBuffer sb = new StringBuffer();
	         sb = sb.append("--");
	         sb = sb.append(BOUNDARY);
	         sb = sb.append("\r\n");
	         sb = sb.append("Content-Disposition: form-data; name=\"FaceDataRecord\"\r\n\r\n");
	         sb = sb.append(json);
	         sb = sb.append("\r\n");
	         // 发送文件:
	         sb = sb.append("--");
	         sb = sb.append(BOUNDARY);
	         sb = sb.append("\r\n");
	         sb = sb.append("Content-Disposition: form-data; name=\"FaceImage\"; filename=\"1.jpg\"\r\nContent-Type: image/jpeg\r\n\r\n");
	         sb = sb.append("Content-Type: application/octet-stream\r\n\r\n");
	         byte[] data = sb.toString().getBytes();
	         byte[] end_data = ("\r\n--" + BOUNDARY + "--\r\n").getBytes();
	         // 设置HTTP头:
	         connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
	         connection.setRequestProperty("Content-Length", String.valueOf(data.length + file.length + end_data.length));
	         // 输出:
	         OutputStream outwritestream = connection.getOutputStream();
	         outwritestream.write(data);
	         outwritestream.write(file);
	         outwritestream.write(end_data);
	         
	         outwritestream.flush();
             outwritestream.close();
	         
	         
//	         if (!StringUtils.isEmpty(json)) {
//	             byte[] writebytes =json.getBytes();
//	             connection.setRequestProperty("Content-Length",String.valueOf(writebytes.length));
//	             OutputStream outwritestream = connection.getOutputStream();
//	             outwritestream.write(json.getBytes());
//	             outwritestream.flush();
//	             outwritestream.close();
//	         }
	         if (connection.getResponseCode() == 200 || connection.getResponseCode() == 201) {
	             in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
	             String line;
	             while ((line = in.readLine()) != null) {
	                 result.append(line);
	             }
	         } else {
	             String errResult = formatResultInfo(connection, type);
	             return errResult;
	         }
	
	         nc = 0;
	     } catch (Exception e) {
	         nc = 0;
	         throw new RuntimeException(e);
	     } finally {
	         try {
	             if (in != null) in.close();
	         } catch (Exception e2) {
	             e2.printStackTrace();
	         }
	     }
	     return result.toString();
	 }
    

    /** 
     * 向指定URL发送GET方法的请求 
     * @param url                                       发送请求的URL
     * @param param                                     请求参数，请求参数应该是 name1=value1&name2=value2 的形式。
     * @param username                                  验证所需的用户名
     * @param password                                  验证所需的密码
     * @param type                                      返回xml和json格式数据，默认xml，传入json返回json数据
     * @return URL 所代表远程资源的响应结果
     */  
    public static String sendGet(String url, String param, String username, String password, String type) {
        StringBuilder result = new StringBuilder();
        BufferedReader in = null;
        try {
            String wwwAuth = sendGet(url, param);//发起一次授权请求
            if (wwwAuth.startsWith("WWW-Authenticate:")) {
                wwwAuth = wwwAuth.replaceFirst("WWW-Authenticate:", "");
            } else {
                return wwwAuth;
            }
            nc ++;
            String urlNameString = url + (StringUtils.isNotEmpty(param) ? "?" + param : "");
            URL realUrl = new URL(urlNameString);
            // 打开和URL之间的连接
            HttpURLConnection connection = (HttpURLConnection)realUrl.openConnection();
            // 设置通用的请求属性
            setRequestProperty(connection, wwwAuth,realUrl, username, password, GET, type);
            // 建立实际的连接
            // connection.connect();
            in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result.append(line);
            }
            nc = 0;
        } catch (Exception e) {
            nc = 0;  
            throw new RuntimeException(e);
        } finally {
            try {
                if (in != null) in.close();
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return result.toString();
    }  
  
    /**  
     * 生成授权信息  
     * @param authorization                             上一次调用返回401的WWW-Authenticate数据
     * @param username                                  用户名
     * @param password                                  密码
     * @return                                          授权后的数据, 应放在http头的Authorization里
     * @throws IOException                              异常
     */  
    private static String getAuthorization(String authorization, String uri, String username, String password, String method) throws IOException {
  
        uri = StringUtils.isEmpty(uri) ? "/" : uri;  
        // String temp = authorization.replaceFirst("Digest", "").trim();
        String temp = authorization.replaceFirst("Digest", "").trim().replace("MD5","\"MD5\"");
        // String json = "{\"" + temp.replaceAll("=", "\":").replaceAll(",", ",\"") + "}";
        String json = withdrawJson(authorization);
        // String json = "{ \"realm\": \"Wowza\", \" domain\": \"/\", \" nonce\": \"MTU1NzgxMTU1NzQ4MDo2NzI3MWYxZTZkYjBiMjQ2ZGRjYTQ3ZjNiOTM2YjJjZA==\", \" algorithm\": \"MD5\", \" qop\": \"auth\" }";

        JSONObject jsonObject = JSON.parseObject(json);  
        // String cnonce = new String(Hex.encodeHex(Digests.generateSalt(8)));    //客户端随机数
        String cnonce = Digests.generateSalt2(8);
        String ncstr = ("00000000" + nc).substring(Integer.toString(nc).length());     //认证的次数,第一次是1，第二次是2...  
        // String algorithm = jsonObject.getString("algorithm");
        String algorithm = jsonObject.getString("algorithm");
        String qop = jsonObject.getString("qop");
        String nonce = jsonObject.getString("nonce");
        String realm = jsonObject.getString("realm");

        String response = Digests.http_da_calc_HA1(username, realm, password,
                nonce, ncstr, cnonce, qop,
                method, uri, algorithm);
  
        //组成响应authorization  
        authorization = "Digest username=\"" + username + "\"," + temp;  
        authorization += ",uri=\"" + uri  
                + "\",nc=\"" + ncstr  
                + "\",cnonce=\"" + cnonce  
                + "\",response=\"" + response+"\"";  
        return authorization;  
    }

    /**
     * 将返回的Authrization信息转成json
     * @param authorization                                     authorization info
     * @return                                                  返回authrization json格式数据 如：String json = "{ \"realm\": \"Wowza\", \" domain\": \"/\", \" nonce\": \"MTU1NzgxMTU1NzQ4MDo2NzI3MWYxZTZkYjBiMjQ2ZGRjYTQ3ZjNiOTM2YjJjZA==\", \" algorithm\": \"MD5\", \" qop\": \"auth\" }";
     */
    private static String withdrawJson(String authorization) {
        String temp = authorization.replaceFirst("Digest", "").trim().replaceAll("\"","");
        // String noncetemp = temp.substring(temp.indexOf("nonce="), temp.indexOf("uri="));
        // String json = "{\"" + temp.replaceAll("=", "\":").replaceAll(",", ",\"") + "}";
        String[] split = temp.split(",");
        Map<String, String> map = new HashMap<>();
        Arrays.asList(split).forEach(c -> {
            String c1 = c.replaceFirst("=",":");
            String[] split1 = c1.split(":");
            map.put(split1[0].trim(), split1[1].trim());
        });
        return JSONObject.toJSONString(map);
    }

    /** 
     * 向指定URL发送GET方法的请求 
     * @param url                                                   发送请求的URL
     * @param param                                                 请求参数，请求参数应该是 name1=value1&name2=value2 的形式。
     * @return URL                                                  所代表远程资源的响应结果
     */  
    public static String sendGet(String url, String param) {  
        StringBuilder result = new StringBuilder();  
        BufferedReader in = null;  
        try {  
  
            String urlNameString = url + (StringUtils.isNotEmpty(param) ? "?" + param : "");
            URL realUrl = new URL(urlNameString);  
            // 打开和URL之间的连接  
            URLConnection connection = realUrl.openConnection();  
            // 设置通用的请求属性  
            connection.setRequestProperty("accept", "*/*");  
            connection.setRequestProperty("connection", "Keep-Alive");  
            connection.setRequestProperty("user-agent",  
                    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");  
  
            connection.connect();  
  
            //返回401时需再次用用户名和密码请求  
            //此情况返回服务器的 WWW-Authenticate 信息  
            if (((HttpURLConnection) connection).getResponseCode() == 401) {  
                Map<String, List<String>> map = connection.getHeaderFields();  
                return "WWW-Authenticate:" + map.get("WWW-Authenticate").get(0);  
            }  
  
            in = new BufferedReader(new InputStreamReader(connection.getInputStream()));  
            String line;  
            while ((line = in.readLine()) != null) {  
                result.append(line);  
            }  
        } catch (Exception e) {  
            throw new RuntimeException("get请求发送失败",e);  
        }  
        // 使用finally块来关闭输入流  
        finally {  
            try {  
                if (in != null) in.close();  
            } catch (Exception e2) {  
                e2.printStackTrace();  
            }  
        }  
        return result.toString();  
    }

    /**
     * HTTP set request property
     *
     * @param connection                            HttpConnection
     * @param wwwAuth                               授权auth
     * @param realUrl                               实际url
     * @param username                              验证所需的用户名
     * @param password                              验证所需的密码
     * @param method                                请求方式
     * @param type                                  返回xml和json格式数据，默认xml，传入json返回json数据
     */
    private static void setRequestProperty(HttpURLConnection connection, String wwwAuth, URL realUrl, String username, String password, String method, String type)
        throws IOException {

        if (type != null && type.equals("json")) {
            // 返回json
            connection.setRequestProperty("accept", "application/json;charset=UTF-8");
            connection.setRequestProperty("Content-Type","application/json;charset=UTF-8");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("user-agent",
                                          "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
        } else {
            // 返回xml
            if (!method.equals(GET)) {
                connection.setRequestProperty("Content-Type","application/json;charset=UTF-8");
            }
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            // connection.setRequestProperty("Cache-Control", "no-cache");
            connection.setRequestProperty("user-agent",
                                          "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");

        }
        //授权信息
        String authentication = getAuthorization(wwwAuth, realUrl.getPath(), username, password, method);
        connection.setRequestProperty("Authorization", authentication);
    }

    /**
     * 格式化请求返回信息，支持json和xml格式
     * @param connection                                HttpConnection
     * @param type                                      指定返回数据格式，json、xml，默认xml
     * @return                                          返回数据
     */
    private static String formatResultInfo(HttpURLConnection connection, String type) throws IOException {
        String result = "";
        if (type != null && type.equals("json")) {
            result = String.format("{\"errCode\":%s, \"message\":%s}",connection.getResponseCode(),connection.getResponseMessage());
        } else {
            result = String.format(" <?xml version=\"1.0\" encoding=\"UTF-8\" ?> "
                                       + " <wmsResponse>"
                                       + " <errCode>%d</errCode>"
                                       + " <message>%s</message>"
                                       + " </wmsResponse>",connection.getResponseCode(),connection.getResponseMessage());
        }
        return result;
    }
}